package com.zuo.demo01.controller.admin;


import com.zuo.demo01.pojo.User;
import com.zuo.demo01.service.ConfigService;
import com.zuo.demo01.service.UserService;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

@Controller
public class LoginController {


    @Autowired
    private UserService userService;


    @Autowired
    private ConfigService configService;


    @GetMapping("/myLogin")
    public String login(){
        return "admin/login";
    }


    @PostMapping("/toLogin")
    public String login(User user, @RequestParam("verifyCode") String verifyCode, HttpServletRequest req, HttpSession session, Model model) {
        //System.out.println(this.getClass().getSimpleName()+"->"+user);
        User loginUser = userService.login(user);
        if(StringUtils.isEmpty(verifyCode)){
            model.addAttribute("msg","验证码不能为空");
            return "admin/login";
        }
        String verify = session.getAttribute("verifyCode")+"";
        if(!verify.equalsIgnoreCase(verifyCode)){
            model.addAttribute("msg","验证码错误");
            return "admin/login";
        }

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());

        try{
            subject.login(token);
            Browser browser = UserAgent.parseUserAgentString(req.getHeader("User-Agent")).getBrowser();
            Version version = browser.getVersion(req.getHeader("User-Agent"));
            String info = browser.getName()+"/"+version.getVersion();
            String ip = InetAddress.getLocalHost().getHostAddress();

            System.out.println(this.getClass().getSimpleName()+"->"+info);
            System.out.println(this.getClass().getSimpleName()+"->"+ip);

            Map<String,String> configs = configService.findAll();
            session.setAttribute("pic",configs.get("pic"));

            session.setAttribute("nickname",loginUser.getNickname());
            session.setAttribute("loginUser",loginUser);
            session.setAttribute("selfemail",configs.get("selfemail"));
            return "redirect:admin/index";
        }catch (UnknownAccountException e){
            model.addAttribute("msg","用户名不存在");
            return "admin/login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "admin/login";
        } catch (UnknownHostException e) {
            model.addAttribute("msg","ip地址有异常");
            return "admin/login";
        }

    }

    @RequestMapping("/noauth.html")
    public String noauth(){
        return "noauth";
    }


}
